package com.joesem.sale.client.logic.login

import android.app.Application
import android.util.Log
import com.igexin.sdk.PushManager
import com.joesem.sale.client.MyApplication
import com.joesem.sale.client.base.BaseViewModel
import com.joesem.sale.client.base.isSuccess
import com.joesem.sale.client.base.request
import com.joesem.sale.client.base.toast
import com.joesem.sale.client.db.DB
import com.joesem.sale.client.network.NetworkTools.apiService
import org.json.JSONObject

class LoginViewModel(application: Application) : BaseViewModel(application) {

    fun login(account: String?, pwd: String?) {
        request(isLoading = true, request = {
            val result = login(account.orEmpty(), pwd.orEmpty())
            if (result.isSuccess()) {
                userGTCid(
                    hashMapOf(
                        "device_id" to PushManager.getInstance().getClientid(getApplication())
                    )
                )
                PushManager.getInstance().turnOnPush(getApplication())
            }
            result
        }, response = {
            actionIntent(LoginSuccessIntent)
        }, responseFailed = {
            toast(it.message.orEmpty())
        })
    }

    object LoginSuccessIntent
}